package com.zoweunion.mechanic.service.base.impl;

import com.alibaba.fastjson.JSONObject;
import com.zoweunion.mechanic.dao.base.RoleDao;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.base.RoleService;
import com.zoweunion.mechanic.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional()
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;

	@Override
	@Transactional(readOnly = true)
	public List<Map<String, Object>> getAll(Map<String, Object> map) {
		return roleDao.getAll(map);
	}

	@Override
	public void update(Map<String, Object> reqMap) {
		roleDao.update(reqMap);
		
	}

	@Override
	@Transactional(readOnly=true)
	public List<Map<String, Object>> getAllPermissions(User currentUser) {
		return roleDao.getAllPermissions(currentUser.getS_id());
	}

	@Override
	@Transactional(readOnly=true)
	public List<Map<String, Object>> getParentPermissions() {
		return roleDao.getParentPermissions();
	}
	@Override
	@Transactional(readOnly=true)
	public List<Map<String, Object>> getChildrenPermissions(Map<String, Object> reqMap) {
		return roleDao.getChildrenPermissions(reqMap);
	}

	@Override
	@Transactional(readOnly=true)
	public List<Map<String, Object>> getAllPermissionsByRole(Map<String, Object> reqMap) {
		return roleDao.getAllPermissionsByRole(reqMap);
	}

	/**
	 * 更新角色的相应权限
	 * 1.删除原有角色_权限关联数据
	 * 2.创建新的权限角色关联数据
	 * @param reqMap
	 */
	@Override
	public void updateRole_permission(Map<String, Object> reqMap) {
		//1根据reqMap中的r_id删除相应的角色_权限关联表中的数据
		roleDao.deleteRole_permissionByR_id(reqMap);
		
		//2.遍历p_ids   创建新的角色_权限关联表数据
		List<String> p_ids = JSONObject.parseObject(reqMap.get("p_ids").toString(), List.class);
		for (String p_id : p_ids) {
			reqMap.put("id", UuidUtil.get32UUID());
			reqMap.put("p_id", p_id);
			roleDao.insertRole_permission(reqMap);
		}
	}
	

	

}
